////////////////////////////////////////////////////////////////////////////////
// Copyright 2006 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   SO
// AUTHOR           :   Victor Zheng 
// DATE CREATE      :   2006/01/10
// DESCRIPTION      :   a DAO file for the function, SO
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2006/01/10   Victor Zheng    1.0                 source is generated.
//
//
//
////////////////////////////////////////////////////////////////////////////////

package com.erp.app.common.itemTask.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.erp.app.common.ProjectConstants;
import com.erp.app.common.itemTask.pk.ItemTaskPk;
import com.erp.app.common.itemTask.vo.ItemTaskVo;
import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.dao.SimpleSearchMaintStyleDao;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.types.CustomBigInteger;
import com.erp.framework.types.CustomSqlDate;

/**
* <p>A <strong>DAO</strong>(Data Access Object) abstracts and encapsulates
* all access to the persistent store.  The DAO manages the connection with the
* data source to obtain and store data.  Also, the DAO implements
* the access mechanism required to work with the data source,
* and always provides a uniform API to its clients.
*
* @author          Victor Zheng 
* @version 1.0     2006/01/10
* @since           FrameWork 1.0
*
*/
public class ItemTaskDao extends SimpleSearchMaintStyleDao {

	/**
	* Returns an array list of child VOs by the parent
	* @param         parent   the incoming parent VO
	* @param         conn     the database connection
	* @return        an array list of child VOs
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	public ChildList selectByParent(SimpleBaseVo parent, Connection conn)
		throws FindRecordException {

		return super.selectByParent(parent, this.getItemTaskClass(), conn);
	}

	protected Class getItemTaskClass() {
        return ItemTaskVo.class;
    }

    /**
	* Constructs a string of SQL insert statement
	* @return        a string of SQL insert statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlInsert() {

		String sql = "INSERT INTO T_ITEM_TASK ("
			+ "COMPANY_CODE, DOCUMENT_NO, DOCUMENT_NO2, DOCUMENT_TYPE, LINE_NO, TASK_CODE, "
			+ "TASK_DESC, USER_GROUP, DEADLINE, COMPLETE_DATE, CREATE_USER, "
			+ "CREATE_DATE, UPDATE_USER, UPDATE_DATE, USER_CODE, EXP_START_DATE, "
			+ "EXP_FINISH_DATE, DURATION, REF_LINE_NO, REF_DURATION, DC_IND, "
			+ "DC_DATE, START_DATE, VARIATIONS, REMARKS, STATUS, QTY, SEQ, TASK_TMPL_CODE) "
			+ "VALUES ("
			+ "?, ?, ?, ?, ?, ?, "
			+ "?, ?, ?, ?, ?, "
			+ "SYSDATE, ?, ?, ?, ?, "
			+ "?, ?, ?, ?, ?, "
			+ "?, ?, ?, ?, DECODE(?, NULL,'"+ProjectConstants.ITEM_TASK_INCOMPLETE+"','"+ProjectConstants.ITEM_TASK_COMPLETE +"'), ?, ?, ?)";
		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL insert or update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForInsertAndModify(PreparedStatement pStmt, Object obj, String opType)
		throws SQLException {

		ItemTaskVo vo = (ItemTaskVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getDocumentNo());
		pStmt.setString(i++, vo.getDocumentNo2());
		pStmt.setString(i++, vo.getDocumentType());
		pStmt.setInt(i++, vo.getTaskLineNo().intValue());
		setNullableString(pStmt, i++, vo.getTaskTaskCode());
		setNullableString(pStmt, i++, vo.getTaskTaskDesc());
		setNullableString(pStmt, i++, vo.getTaskUserGroup());
		setNullableDate(pStmt, i++, vo.getTaskDeadline());
		setNullableDate(pStmt, i++, vo.getTaskCompleteDate());
		setNullableString(pStmt, i++, vo.getCreateUser());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getCreateDate());

		setNullableString(pStmt, i++, vo.getUpdateUser());

		if (ProjectConstants.DB_OP_INSERT.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		setNullableString(pStmt, i++, vo.getTaskUserCode());
		setNullableDate(pStmt, i++, vo.getTaskExpStartDate());
		setNullableDate(pStmt, i++, vo.getTaskExpFinishDate());
		setNullableBigInteger(pStmt, i++, vo.getTaskDuration());
		setNullableBigInteger(pStmt, i++, vo.getTaskRefLineNo());
		setNullableString(pStmt, i++, vo.getTaskRefDuration());

		setNullableString(pStmt, i++, vo.getTaskDcInd());
		setNullableDate(pStmt, i++, vo.getTaskDcDate());
		setNullableDate(pStmt, i++, vo.getTaskStartDate());
		setNullableString(pStmt, i++, vo.getTaskVariations());
		setNullableString(pStmt, i++, vo.getTaskRemarks());
		
		setNullableDate(pStmt, i++, vo.getTaskCompleteDate());
		setNullableBigInteger(pStmt, i++, vo.getTaskQty());
		setNullableString(pStmt, i++, vo.getTaskSeq());
		setNullableString(pStmt, i++, vo.getTaskTaskTmplCode());
//		setNullableString(pStmt, i++, vo.getTaskStatus());
		
		
		if(ProjectConstants.DB_OP_UPDATE.equals(opType)){
			pStmt.setString(i++, vo.getCompanyCode());
			pStmt.setString(i++, vo.getDocumentNo());
			pStmt.setString(i++, vo.getDocumentNo2());
			pStmt.setString(i++, vo.getDocumentType());
			pStmt.setInt(i++, vo.getTaskLineNo().intValue());
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		}
	}

	/**
	* Sets the values for a SQL insert statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForInsert(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_INSERT);
	}

	/**
	* Constructs a string of SQL update statement
	* @return        a string of SQL update statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlUpdate() {

		String sql = "UPDATE T_ITEM_TASK SET COMPANY_CODE=?, DOCUMENT_NO=?, DOCUMENT_NO2=?, DOCUMENT_TYPE=?, LINE_NO=?, "
			+ "TASK_CODE=?, "
			+ "TASK_DESC=?, USER_GROUP=?, DEADLINE=?, COMPLETE_DATE=?, CREATE_USER=?, "
			+ "CREATE_DATE=?, UPDATE_USER=?, UPDATE_DATE=SYSDATE, USER_CODE=?, EXP_START_DATE=?, "
			+ "EXP_FINISH_DATE=?, DURATION=?, REF_LINE_NO=?, REF_DURATION=?, DC_IND=?, DC_DATE=?, "
			+ "START_DATE=?, VARIATIONS=?, REMARKS=?, STATUS=DECODE(?,NULL,'"+ProjectConstants.ITEM_TASK_INCOMPLETE+"','"+ProjectConstants.ITEM_TASK_COMPLETE +"'), QTY=?, SEQ=?, TASK_TMPL_CODE=? "
			+ "WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_NO2=? AND DOCUMENT_TYPE=? AND LINE_NO=? "
			+ "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForModify(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_UPDATE);
	}

	/**
	* Constructs a string of SQL delete statement
	* @return        a string of SQL update statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlDelete() {
		String sql = "DELETE FROM T_ITEM_TASK WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_NO2=? AND DOCUMENT_TYPE=? AND LINE_NO=? ";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL delete statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForDelete(PreparedStatement pStmt, Object obj)
		throws SQLException {

		ItemTaskVo vo = (ItemTaskVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getDocumentNo());
		pStmt.setString(i++, vo.getDocumentNo2());
		pStmt.setString(i++, vo.getDocumentType());
		pStmt.setInt(i++, vo.getTaskLineNo().intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlFind() {

		String sql = "SELECT COMPANY_CODE, DOCUMENT_NO, DOCUMENT_NO2, DOCUMENT_TYPE, LINE_NO, TASK_CODE, "
			+ "TASK_DESC, USER_GROUP, DEADLINE, COMPLETE_DATE, CREATE_USER, "
			+ "CREATE_DATE, UPDATE_USER, UPDATE_DATE, USER_CODE, EXP_START_DATE, "
			+ "EXP_FINISH_DATE, DURATION, REF_LINE_NO, REF_DURATION, DC_IND, "
			+ "DC_DATE, START_DATE, VARIATIONS, REMARKS, STATUS, QTY, SEQ, TASK_TMPL_CODE "
			+ "FROM T_ITEM_TASK "
			+ "WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_NO2=? AND DOCUMENT_TYPE=? AND LINE_NO=? "
			+ "ORDER BY COMPANY_CODE, DOCUMENT_NO, DOCUMENT_TYPE, LINE_NO";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding by primary key.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming PK
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForFind(PreparedStatement pStmt, Object obj)
		throws SQLException {

		ItemTaskPk pk = (ItemTaskPk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setString(i++, pk.getDocumentNo());
		pStmt.setString(i++, pk.getDocumentNo2());
		pStmt.setString(i++, pk.getDocumentType());
		pStmt.setInt(i++, pk.getTaskLineNo().intValue());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected String sqlList() {

		String sql = "SELECT COMPANY_CODE, DOCUMENT_NO, DOCUMENT_NO2, DOCUMENT_TYPE, LINE_NO, TASK_CODE, "
			+ "TASK_DESC, USER_GROUP, DEADLINE, COMPLETE_DATE, CREATE_USER, "
			+ "CREATE_DATE, UPDATE_USER, UPDATE_DATE, USER_CODE, EXP_START_DATE, "
			+ "EXP_FINISH_DATE, DURATION, REF_LINE_NO, REF_DURATION, DC_IND, "
			+ "DC_DATE, START_DATE, VARIATIONS, REMARKS, STATUS, QTY, SEQ, TASK_TMPL_CODE "
			+ "FROM T_ITEM_TASK "
			+ "WHERE COMPANY_CODE=? AND DOCUMENT_NO=? AND DOCUMENT_NO2=? AND DOCUMENT_TYPE=? "
			+ "ORDER BY DECODE (sf_is_number(seq), 'YES', LPAD(seq, 10, '0'), LPAD(seq, 10, '@')), COMPANY_CODE, DOCUMENT_NO, DOCUMENT_NO2, DOCUMENT_TYPE, LINE_NO";

		//super.logSqlQuery(sql);
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding child tables.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming parent PK
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected void setValuesForList(PreparedStatement pStmt, Object obj)
		throws SQLException {

        // Please create a subclass of ItemTaskDao for each Module like BodyTaskDao

//		if (obj instanceof SoHdrPk) {
//			SoHdrPk pk = (SoHdrPk) obj;
//            int i = 1;
//            pStmt.setString(i++, pk.getCompanyCode());
//            pStmt.setString(i++, pk.getSoNo());
//            pStmt.setString(i++, ProjectConstants.DOC_TYPE_SO);
//		} else if (obj instanceof PoHdrPk) {
//			PoHdrPk pk = (PoHdrPk) obj;
//            int i = 1;
//            pStmt.setString(i++, pk.getCompanyCode());
//            pStmt.setString(i++, pk.getPoNo());
//            pStmt.setString(i++, ProjectConstants.DOC_TYPE_PO);			
//		} else if (obj instanceof QuotHdrPk) {
//			QuotHdrPk pk = (QuotHdrPk) obj;
//            int i = 1;
//            pStmt.setString(i++, pk.getCompanyCode());
//            pStmt.setString(i++, pk.getQuotNo());
//            pStmt.setString(i++, ProjectConstants.DOC_TYPE_QUOT);
//		} else if (obj instanceof InvoiceHdrPk) {
//			InvoiceHdrPk pk = (InvoiceHdrPk) obj;
//            int i = 1;
//            pStmt.setString(i++, pk.getCompanyCode());
//            pStmt.setString(i++, pk.getInvoiceNo());
//            pStmt.setString(i++, ProjectConstants.DOC_TYPE_INV);
//		} else 
	    
	}

	/**
	* Sets the values of a result set to a vo
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Victor Zheng 
	* @since      FrameWork 1.0
	*/
	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {

		ItemTaskVo vo = this.makeItemTaskVo();

		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setDocumentNo(rs.getString("DOCUMENT_NO"));
		vo.setDocumentNo2(rs.getString("DOCUMENT_NO2"));
		vo.setDocumentType(rs.getString("DOCUMENT_TYPE"));
		vo.setTaskLineNo(CustomBigInteger.customize(rs.getBigDecimal("LINE_NO")));
		vo.setTaskTaskCode(rs.getString("TASK_CODE"));
		vo.setTaskTaskDesc(rs.getString("TASK_DESC"));
		vo.setTaskUserGroup(rs.getString("USER_GROUP"));
		vo.setTaskDeadline(CustomSqlDate.customize(rs.getDate("DEADLINE")));
		vo.setTaskCompleteDate(CustomSqlDate.customize(rs.getDate("COMPLETE_DATE")));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
		vo.setTaskUserCode(rs.getString("USER_CODE"));
		vo.setTaskExpStartDate(CustomSqlDate.customize(rs.getDate("EXP_START_DATE")));
		vo.setTaskExpFinishDate(CustomSqlDate.customize(rs.getDate("EXP_FINISH_DATE")));
		vo.setTaskDuration(CustomBigInteger.customize(rs.getBigDecimal("DURATION")));
		vo.setTaskRefLineNo(CustomBigInteger.customize(rs.getBigDecimal("REF_LINE_NO")));
		vo.setTaskRefDuration(rs.getString("REF_DURATION"));
		vo.setTaskDcInd(rs.getString("DC_IND"));
		vo.setTaskDcDate(CustomSqlDate.customize(rs.getDate("DC_DATE")));
		vo.setTaskStartDate(CustomSqlDate.customize(rs.getDate("START_DATE")));
		vo.setTaskVariations(rs.getString("VARIATIONS"));
		vo.setTaskRemarks(rs.getString("REMARKS"));
		vo.setTaskStatus(rs.getString("STATUS"));
		vo.setTaskQty(CustomBigInteger.customize(rs.getBigDecimal("QTY")));
		vo.setTaskSeq(rs.getString("SEQ"));
		vo.setTaskTaskTmplCode(rs.getString("TASK_TMPL_CODE"));
		
		return vo;
	}

	protected ItemTaskVo makeItemTaskVo() {
        return new ItemTaskVo();
    }

}
